﻿using System;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml;
using System.IO;

namespace WXR2BlogML
{
	/// <summary>
	/// define BLOGML25 to use extended import functions
	/// </summary>
	class Program
	{
		/// <summary>
		/// Mains the specified args.
		/// arg0 = filename
		/// arg1 = [(empty)|blogml25]
		/// </summary>
		/// <param name="args">The args.</param>
		static void Main(string[] args)
		{

			if (args == null || args.Length != 1)
			{
				Console.WriteLine(@"I need a file to convert");
				Wait();
				return;
			}

			string file = args[0];
			FileInfo source = new FileInfo(file);
			if (!source.Exists)
			{
				Console.WriteLine(@"File not found: " + source.FullName);
				Wait();
				return;
			}

			// the blogml type, specify 'blogml25' for extended inport funciotn
			
			if (args.Length > 1)
				BlogMlSettings.Blogmltype = args[1];

			string target = Path.Combine(source.DirectoryName,
												  Path.GetFileNameWithoutExtension(source.Name) + "_BlogML" +
												  Path.GetExtension(source.Name));

			FileInfo targetFile = new FileInfo(target);
			if (targetFile.Exists)
			{
				Console.WriteLine(@"Press 'y' to overwrite existing file: " + target);
				if (Console.ReadKey().KeyChar != 'y')
				{
					Wait();
					return;
				}
			}
			//try
			//{

			// process the file and convert the values
			string destfile = file.Replace(".xml", ".converted.xml");
			// XhtmlToXmlUtils.XhtmlToXml(file, destfile);
			WordpressExportFix.FixExportFile(file, destfile);

			XElement channelWxr = XElement.Load(destfile).Element("channel");
			DateTime created = WxrBlogMlExporter.ParseWPDate(channelWxr.Element("pubDate").Value);

			XElement allCats = WxrBlogMlExporter.CreateCategories(channelWxr);
			XElement allAuthors = WxrBlogMlExporter.CreateAuthors(channelWxr);


			XElement blog = new XElement(WxrBlogMlExporter.blogMLNs + "blog",
				 new XAttribute("date-created", WxrBlogMlExporter.FormatWpDate(created)),
				 new XAttribute("root-url", "/"),
				 new XElement(WxrBlogMlExporter.blogMLNs + "title",
					  new XAttribute("type", "text"), new XCData(channelWxr.Element("title").Value)),
				 new XElement(WxrBlogMlExporter.blogMLNs + "sub-title",
					  new XAttribute("type", "text"), new XCData(channelWxr.Element("description").Value)),
				 allAuthors,
				 WxrBlogMlExporter.CreateExtendedProperties(),
				 allCats
				//#if BLOGML25
				//                 WxrBlogMlExporter.CreateTags(channelWxr), //only in blogml 2.5 I need 2.0 for blogengine
				//#endif
				// WxrBlogMlExporter.CreatePosts(channelWxr, allAuthors, allCats)
					  );
			if (BlogMlSettings.Blogmltype == BlogMlSettings.Blogml25)
				blog.Add(WxrBlogMlExporter.CreateTags(channelWxr));
			blog.Add(WxrBlogMlExporter.CreatePosts(channelWxr, allAuthors, allCats));

			XDocument blogMl = new XDocument(blog);
			blogMl.Save(target);
			Console.WriteLine(@"Saved file: " + target);

			bool errors = false;
			XmlSchemaSet schema = new XmlSchemaSet();
			if (BlogMlSettings.Blogmltype == BlogMlSettings.Blogml25)
				schema.Add(WxrBlogMlExporter.blogMLNs.NamespaceName, XmlReader.Create("BlogMl25.xsd"));
			else
				schema.Add(WxrBlogMlExporter.blogMLNs.NamespaceName, XmlReader.Create("BlogMl.xsd"));

			blogMl.Validate(schema, ((sender, e) =>
											 {
												 errors = true;
												 Console.WriteLine(e.Message);
											 }));
			if (errors)
			{
				Console.WriteLine(@"---------------");
				Console.WriteLine(@"BlogML is not valid!");
				Console.WriteLine(@"---------------");
			}
			//}
			//catch(Exception ex)
			//{
			//    Console.WriteLine(ex.ToString());
			//}
			Wait();
		}

		public static void Wait()
		{
			Console.WriteLine();
			Console.WriteLine(@"Press Enter to exit ...");
			Console.ReadLine();
		}
	}
}
